package com.lk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.bean.Grade;
import com.lk.service.GradeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/grade")
public class GradeController {

    @Resource
    private GradeService gradeService;


    /**
     * 跳转年级列表页面视图
     *
     * @return
     */
    @GetMapping("/goGradeListView")
    public String goGradeListView() {
        return "/grade/gradeList";
    }


    /**
     * 根据ids删除年级
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteGrade")
    @ResponseBody
    public Map<String,Object> deleteGrade(@RequestParam(value = "ids[]",required = true) Integer[] ids){
        Map<String,Object> result = new HashMap<>();
        Integer row = gradeService.deleteGrade(ids);
        if (row>0){
            result.put("success",true);
        }else {
            result.put("success",false);
            result.put("msg","删除失败");
        }
        return result;
    }


    /**
     * 添加年级信息
     *
     * @param grade
     * @return
     */
    @PostMapping("/addGrade")
    @ResponseBody
    public Map<String,Object> addGrade(Grade grade){
        Map<String,Object> result = new HashMap<>();
        //判断请求参数是否为空
        if (grade == null){
            result.put("success",false);
            result.put("msg","请求参数为空");
            return result;
        }
        //根据名称判断要添加的年级名是否存在
        String name = grade.getName();
        Grade grade1 = gradeService.findByName(name);
        if (grade1 != null){
            result.put("success",false);
            result.put("msg","年级名已经存在");
        }
        int row = gradeService.addGrade(grade);
        if (row ==1){
            result.put("success",true);
        }else {
            result.put("success",false);
            result.put("msg","添加失败");
        }
        return result;
    }


    /**
     * 修改年级信息
     *
     * @param grade
     * @return
     */
    @PostMapping("/editGrade")
    @ResponseBody
    public Map<String,Object> editGrade(Grade grade){
        Map<String,Object> result = new HashMap<>();
        //根据name查询是否已经存在相同的年级
        Grade grade1 = gradeService.findByName(grade.getName());
        //自己
        if (grade1 != null && grade1.getId() == grade.getId()) {
            int row = gradeService.updateGrade(grade);
            if (row == 1) {
                result.put("success", true);
                return result;
            } else {
                result.put("success", false);
                result.put("msg", "修改失败");
                return result;
            }
        } else if (grade1 != null && grade1.getId() != grade.getId()) { //名字存在但不是自己
            result.put("success", false);
            result.put("msg", "用户名已存在");
            return result;
        } else { //年级名不存在
            int row = gradeService.updateGrade(grade);
            if (row == 1) {
                result.put("success", true);
                return result;
            } else {
                result.put("success", false);
                result.put("msg", "修改失败");
                return result;
            }
        }
    }


    /**
     * 分页获取grade数据
     *
     * @param page
     * @param rows
     * @param gradename
     * @return
     */
    @PostMapping("/getGradeList")
    @ResponseBody
    public Map<String, Object> getGradeList(Integer page, Integer rows, String gradename) {
        Map<String, Object> result = new HashMap<>();
        Grade grade = new Grade();
        grade.setName(gradename);
        //设置每页记录数
        PageHelper.startPage(page, rows);
        //获取指定的年级信息，若没有则获取全部数据
        List<Grade> list = gradeService.getGradeList(grade);
        PageInfo<Grade> pageInfo = new PageInfo<>(list);

        //总页数
        long total = pageInfo.getTotal();

        result.put("total", total);
        result.put("rows", list);
        return result;
    }


}
